package net.ywd.xp1024search.mapper;

import lombok.extern.java.Log;
import net.ywd.xp1024search.entity.UsdtCnyResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.URLEncoder;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

@Log
@Component
public class GateIoConnection {

    public void saveUsdtCny(UsdtCnyResult usdtCnyResult) throws SQLException {

        Connection conn = null;
//        String conn_str = "jdbc:mysql://192.168.201.22:3306/gate_io?user=mvc&password=" + URLEncoder.encode("mvc123$%^")  + "&useUnicode=true&characterEncoding=UTF8";
        String conn_str = "jdbc:mysql://127.0.0.1:3306/gate_io?user=root&password=" + URLEncoder.encode("ywd")  + "&useUnicode=true&characterEncoding=UTF8";
        try {
            Class.forName("com.mysql.jdbc.Driver");// 动态加载mysql驱动

            // 一个Connection代表一个数据库连接
            conn = DriverManager.getConnection(conn_str);
            Statement stmt = conn.createStatement();
            String sql = "insert into usdt_cny values (null, %s, %s, %s, now())";
            stmt.execute(String.format(sql, usdtCnyResult.getAppraised_rates().getBuy_rate(), usdtCnyResult.getAppraised_rates().getSell_rate(), usdtCnyResult.getAppraised_rates().getRate_24h_ago()));
        } catch (SQLException e) {
            log.warning("MySQL ERROR");
        } catch (Exception e) {
            log.warning("MySQL ERROR");
        } finally {
            conn.close();
        }
    }
}
